using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class forums_controls_administration_ModifyForumCategories : System.Web.UI.UserControl
{
    private void Refresh()
    {
        gridCategories.DataSource = DAL.ForumCategories.All();
        gridCategories.DataBind();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Refresh();
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Managers.UserLoginManager.CurrentUser != null &&
            Managers.UserLoginManager.CurrentUser.HasPermissions(DAL.User.UserStatus.Administrator))
        {
            if (txtName.Text.Trim().Length > 0)
            {
                DAL.ForumCategory category = new DAL.ForumCategory();
                category.Name = txtName.Text;
                DAL.ForumCategories.Add(category);
                txtName.Text = "";

                Refresh();
            }
        }
    }

    protected void SaveCategory(object sender, CommandEventArgs e)
    {
        if (Managers.UserLoginManager.CurrentUser != null &&
            Managers.UserLoginManager.CurrentUser.HasPermissions(DAL.User.UserStatus.Administrator))
        {
            long id;
            if (long.TryParse(e.CommandArgument.ToString(), out id))
            {
                Control parent = (sender as Control).Parent;
                if (parent != null)
                {
                    TextBox txtCurrentName = parent.FindControl("txtName") as TextBox;
                    if (txtCurrentName != null && txtCurrentName.Text.Trim().Length > 0)
                    {
                        DAL.ForumCategory category = DAL.ForumCategories.Item(id);
                        category.Name = txtCurrentName.Text;
                        DAL.ForumCategories.Save(category);
                        Refresh();
                    }
                }
            }
        }
    }
}
